let loop = true;
let mBannerAd = null;
let interval = null;


var QQAds = cc.Class({


    statics: {
      
        /**
         * 播放banner
         */
        showBanner:function(isLoop){
            if(isLoop) {
                loop = isLoop;
            }
            var self = this;
            var oldBannerAd = null;
            if (mBannerAd) {
                oldBannerAd = mBannerAd;
                mBannerAd = null;
            }
            mBannerAd = BK.Advertisement.createBannerAd({
                viewId: ssgConfig.bannerId,
                style: {
                    x: 0,
                    y: 0
                }
            });
            mBannerAd.onLoad(function () {
            //    Log.debug('QQbanner广告加载成功')
            });
            mBannerAd.onError(function (err) {
                //加载失败
                var msg = err.msg;
                var code = err.code;
                Log.error('广告加载失败 msg = ' + msg + ' code = ',code);

            });
            mBannerAd.show();
            if (interval) {
                clearTimeout(interval);
            }
            if(loop){
                interval = setTimeout(function(){
                    self.showBanner(false);
                },6000)
            }
            if (oldBannerAd) {
                oldBannerAd.destory();
            }
        },


        /**
         * 隐藏广告
         */
        hideBanner: function () {
            if (mBannerAd) {
                mBannerAd.hide();
                loop = false;
                if (interval) {
                    clearTimeout(interval);
                    interval = null;
                }
            }
        },

        showVideo: function (callback) {
            Log.info('播放QQ视频广告')

            var videoAd = BK.Advertisement.createVideoAd();
            var isComplete = false;
            videoAd.onLoad(function () {
                videoAd.show();
                //加载成功
                BK.Script.log(1, 1, "onLoad")
            });

            videoAd.onPlayStart(function () {
                //开始播放
                // callback(true);
                BK.Script.log(1, 1, "onPlayStart")
            });

            videoAd.onPlayFinish(function () {
                BK.Script.log(1, 1, "onPlayFinish")
                // callback(true);
                isComplete = true;
                //console.log('视频看完了！！')
            });

            videoAd.onError(function (err) {
                //加载失败
                Log.info('播放视频失败 err = ',err.code + err.msg);
                // BK.Script.log(1, 1, "onError code:" + err.code + " msg:" + err.msg);
                callback(false);
            });

            videoAd.onClose(function () {
                SystemInfo.setIsPlayingVideo(false)
                SoundUtil.resumeMusic();
                callback(isComplete);
            });


        },

    }
});

module.exports = QQAds;